package com.xrui.hbase.codec;

import com.google.common.base.Preconditions;
import com.moz.kiji.delegation.Lookups;
import com.moz.kiji.delegation.NamedProvider;
import com.xrui.hbase.RowKeyComponents;
import com.xrui.hbase.schema.RowKeyFormat;

import java.io.IOException;

/**
 * Interface for row key encoders.
 * <p>
 * A row key encoder is configured to encode precisely one specific row of table and knows exactly the type
 * of data it is supposed to encode.
 * <p>
 * RowKey encoders are instantiated via {@link RowKeyEncoder.Factory}.
 * <p>
 * Intended for developers only.
 */

public interface RowKeyEncoder extends NamedProvider {
    /**
     * Encodes the specified row key.
     *
     * @param components components to encode.
     * @param format     format spec of the row key.
     * @return the binary encoding of the row key.
     * @throws IOException on I/O error.
     */
    byte[] encode(RowKeyComponents components, RowKeyFormat format) throws IOException;

    class Factory {
        /**
         * Creates a new RowKeyEncoder with specified encoder provider name
         *
         * @param name of the encoder
         * @return new instance of the encoder
         */
        public static RowKeyEncoder create(String name) {
            Preconditions.checkNotNull(name);
            return Lookups.getNamed(RowKeyEncoder.class).lookup(name);
        }
    }
}
